/*
 * @lc app=leetcode.cn id=110 lang=csharp
 *
 * [110] 平衡二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public bool IsBalanced(TreeNode root) {
        Queue<TreeNode> queue = new Queue<TreeNode>();
        queue.Enqueue(root);
        while(queue.Count > 0){
            TreeNode node = queue.Dequeue();
            if(node == null){
                continue;
            }
            if(Math.Abs(GetHeight(node.left) - GetHeight(node.right)) > 1){
                return false;
            }
            queue.Enqueue(node.left);
            queue.Enqueue(node.right);
        }
        return true;
    }
    
    private int GetHeight(TreeNode node){
        if(node == null){
            return 0;
        }
        return Math.Max(GetHeight(node.left), GetHeight(node.right)) + 1;
    }

}
// @lc code=end

